Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

267
Vistas
Hiding 3 elements in sequence not working using JavaScript "transitionend" event?

3 boxes that explain the issue

WHAT. I have 3 boxes: blue, red and black and I'm trying to transition them one after another, using JavaScript event listeners, in the following order: black, red and then blue.

HOW: https://codepen.io/gremo/pen/XWVeQVP?editors=1010

Basic HTML structure and nesting (pen for the complete example):

<div id="container">
    <div id="blue"></div>
    <div id="red">
        <div id="black"></div>
    </div>
</div>

JavaScript code is very simple:

const blue = document.getElementById('blue');
const red = document.getElementById('red');
const black = document.getElementById('black');

const hideElement = element => {
  console.log(`Hiding element ${element.getAttribute('id')}`);
  element.classList.add(element.dataset.hideClass);
};

black.addEventListener('transitionend', () => hideElement(red)); // when black transition ends, hide the red
red.addEventListener('transitionend', () => hideElement(blue)); // when red transition ends, hide the blue
hideElement(black); // hide the black (start the chain)

THE PROBLEM: after the black ends, blue and red start at the same time... and this is wrong because blue should start hiding after red completes. In addition, console show that the transitionend event listener for blue is called 2 times.

Any help is much appreciated, I've be struggling with this problem since days.

Console errors

about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

It seems that because black is a child of red, the transitionend event got bubbled up, causing red to fire the event twice:

document.addEventListener('DOMContentLoaded', () => {
  const blue = document.getElementById('blue');
  const red = document.getElementById('red');
  const black = document.getElementById('black');

  const hideElement = element => {
    console.log(`Hiding element ${element.getAttribute('id')}`);
    element.classList.add(element.dataset.hideClass);
  };

  black.addEventListener('transitionend', (e) => {
    e.stopPropagation();// <-- added this line
    hideElement(red);
  });
  red.addEventListener('transitionend', () => hideElement(blue));
  hideElement(black);
});

(Edited from your codepen; I can't fork it because I'm too lazy to register)

about 4 years ago · Juan Pablo Isaza Denunciar

0

transitionend event will trigger all the listeners once executed. As red and black both register the event listener before the first event triggers both a run. To prevent this, you could either pass a function which will add the event listener inside the hide element function or more preferably pass the event alongside the element and call stopPropagation. stopPropagation is an event function which will prevent the propagation of the event, thus only triggering the first one in this case

e?.stopPropagation()

Below is a modified function which should work in your context

document.addEventListener('DOMContentLoaded', () => {
  const blue = document.getElementById('blue');
  const red = document.getElementById('red');
  const black = document.getElementById('black');

  const hideElement = (element,e) => {
    console.log(`Hiding element ${element.getAttribute('id')}`);
    e?.stopPropagation()
    element.classList.add(element.dataset.hideClass);
  };
  black.addEventListener('transitionend', (e) => hideElement(red,e));
  red.addEventListener('transitionend', (e) => hideElement(blue,e));
  hideElement(black);
});
about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda